<template>
  <div class="auth-container">
    <div class="auth-box">
      <h2 class="title">{{ isLogin ? '登录' : '注册' }}</h2>

      <el-form
        :model="form"
        :rules="rules"
        ref="authForm"
        label-position="top"
        class="auth-form"
        @submit.prevent="onSubmit"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="username" clearable />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            autocomplete="new-password"
            clearable
          />
        </el-form-item>

        <!-- 注册表单额外字段，例如确认密码，这里简单示范 -->
        <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            autocomplete="new-password"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="submit-btn" @click="onSubmit">
            {{ isLogin ? '登录' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="switch-text">
        <span>
          {{ isLogin ? '没有账号？' : '已有账号？' }}
          <button class="switch-btn" @click="toggleForm">
            {{ isLogin ? '去注册' : '去登录' }}
          </button>
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {loginApi, registApi} from "@/api/user";
import {ElMessage} from "element-plus";
import {useUserStore} from "@/store/module/user";
import router from "@/router/index";

const isLogin = ref(true)
const userStore = useUserStore();

const form = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

const rules = reactive({
  username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
  confirmPassword: [
    {
      required: true,
      message: '请确认密码/两次输入的密码不一致',
      trigger: 'blur',
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请确认密码/两次输入的密码不一致'))
        } else if (value !== form.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }
    }
  ]
})

const authForm = ref(null)

function toggleForm() {
  isLogin.value = !isLogin.value

  // 重置表单及校验状态
  authForm.value.resetFields()
}

function onSubmit() {
  authForm.value.validate((valid) => {
    if (valid) {
      if (isLogin.value) {
        userStore.login(form.username, form.password).then(res => {
          router.push('/')
        })
      } else {
        registApi({
          username: form.username,
          password: form.password
        }).then(res => {
          if (res)
            ElMessage.success('注册成功！')
          toggleForm()
        })
      }
    } else {
      return false
    }
  })
}
</script>

<style>
.auth-container {
  width: 380px;
  margin: 100px auto;
  padding: 40px 30px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  user-select: none;
}

.auth-box {
  width: 100%;
}

.title {
  text-align: center;
  font-size: 24px;
  color: #409eff;
  margin-bottom: 30px;
}

.auth-form {
  width: 100%;
}

.submit-btn {
  width: 100%;
}

.switch-text {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}

.switch-btn {
  background: none;
  border: none;
  color: #409eff;
  cursor: pointer;
  font-size: 14px;
  padding: 0;
  margin-left: 6px;
  text-decoration: underline;
}

.switch-btn:hover {
  color: #66b1ff;
}
</style>
